
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// Log_LoginLog Data Class
    /// </summary>
    public partial class Log_LoginLogDAL : BaseDAL
    {
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Log_LoginLog";

        public Log_LoginLogDAL()
        {}

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Log_LoginLog model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("insert into [Log_LoginLog](");
            strSql.Append("[LoginID],[CreateDate],[MemberAccount],[LoginIP],[UserAgent],[IsSuccess],[LoginType],[Memo]");
            strSql.Append(") values (");
            strSql.Append("@LoginID,@CreateDate,@MemberAccount,@LoginIP,@UserAgent,@IsSuccess,@LoginType,@Memo");
            strSql.Append(")");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@LoginID", model.LoginID),
                    new SqlParameter("@CreateDate", model.CreateDate),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@LoginIP", model.LoginIP),
                    new SqlParameter("@UserAgent", model.UserAgent),
                    new SqlParameter("@IsSuccess", model.IsSuccess),
                    new SqlParameter("@LoginType", model.LoginType),
                    new SqlParameter("@Memo", model.Memo)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Log_LoginLog model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("update [Log_LoginLog] set ");
            strSql.Append("[CreateDate]=@CreateDate,[MemberAccount]=@MemberAccount,[LoginIP]=@LoginIP,[UserAgent]=@UserAgent,[IsSuccess]=@IsSuccess,[LoginType]=@LoginType,[Memo]=@Memo");
            strSql.Append(" where [LoginID]=@LoginID");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@LoginID", model.LoginID),
                    new SqlParameter("@CreateDate", model.CreateDate),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@LoginIP", model.LoginIP),
                    new SqlParameter("@UserAgent", model.UserAgent),
                    new SqlParameter("@IsSuccess", model.IsSuccess),
                    new SqlParameter("@LoginType", model.LoginType),
                    new SqlParameter("@Memo", model.Memo)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Guid LoginID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Log_LoginLog] ");
            strSql.Append(" where [LoginID]=@LoginID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("@LoginID", LoginID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Log_LoginLog Get(Guid LoginID)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select  top 1 * from [Log_LoginLog] ");
            strSql.Append(" where [LoginID]=@LoginID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("LoginID", LoginID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Log_LoginLog Get(DataRow row)
        {
            if (row == null) return null;

            Log_LoginLog model = new Log_LoginLog();
	
            
            if (row["LoginID"] != DBNull.Value)
            {
                model.LoginID = (Guid)row["LoginID"];
            }      
            
            if (row["CreateDate"] != DBNull.Value)
            {
                model.CreateDate = (DateTime)row["CreateDate"];
            }      
            
            if (row["MemberAccount"] != DBNull.Value)
            {
                model.MemberAccount = (String)row["MemberAccount"];
            }      
            
            if (row["LoginIP"] != DBNull.Value)
            {
                model.LoginIP = (String)row["LoginIP"];
            }      
            
            if (row["UserAgent"] != DBNull.Value)
            {
                model.UserAgent = (String)row["UserAgent"];
            }      
            
            if (row["IsSuccess"] != DBNull.Value)
            {
                model.IsSuccess = (Boolean)row["IsSuccess"];
            }      
            
            if (row["LoginType"] != DBNull.Value)
            {
                model.LoginType = (Int32)row["LoginType"];
            }      
            
            if (row["Memo"] != DBNull.Value)
            {
                model.Memo = (String)row["Memo"];
            }      
            

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select * from [Log_LoginLog] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * FROM [Log_LoginLog] ");
			
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if(filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
			
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Log_LoginLog] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Log_LoginLog]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by CreateDate desc ", out count, cmdParms);
            total = count;
            return dt;
        }
      #endregion  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string MemberAccount)
        {
            string sql = @"SELECT count(1) FROM Log_LoginLog 
                            WHERE MemberAccount = @MemberAccount AND CreateDate >= Convert(char(8),GETDATE(),112) AND CreateDate <= GETDATE() ";

            SqlParameter[] parameters = { new SqlParameter("@MemberAccount", SqlDbType.NVarChar, 50) };
            parameters[0].Value = MemberAccount;

            return DbHelperSQL.Exists(sql, parameters);
        }

        /// <summary>
        /// 获取用户最后一次登录信息
        /// </summary>
        /// <param name="memberAccount"></param>
        /// <returns></returns>
        public Log_LoginLog GetLastLoginInfo(string memberAccount)
        {
            string sql = string.Format(@"select top 1  *
                                            from
                                            (
                                            select top 2 * from [dbo].[Log_LoginLog]
                                            where MemberAccount='{0}' and IsSuccess=1 and LoginType=2
                                            order by CreateDate desc
                                            ) as tempTable
                                            order by CreateDate asc", memberAccount);

            DataSet ds = DbHelperSQL.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return new Log_LoginLog();
            }
        }

        public string LastLoginID(string memberAccount)
        {
            string sql = string.Format(@"select top 1 LoginID from [dbo].[Log_LoginLog]
                                        where MemberAccount='{0}' and LoginType=2 and IsSuccess=1
                                        order by CreateDate desc", memberAccount);
            var id = DbHelperSQL.GetSingle(sql);
            return id == null ? "" : id.ToString();
        }
    }
}


  